package ogmios.components.archivers;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.zip.*;
import java.util.jar.*;
import ogmios.core.*;
/**
 * An archiver that creates a jar file.
 */
public class JarArchiver extends ZipArchiver
{
 public void archive(File outputFile, TreeModel files, Map<String, Object> properties) throws Throwable
 {
   running=true;
   Manifest man = new Manifest();
   Attributes attributes = man.getMainAttributes();
   attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
   for (String line : ((String)properties.get("Manifest")).split("[\\r\\n]+"))
   {
     int start = line.indexOf(":");
     if (start > 0 && start < line.length() - 1)
     {
       attributes.putValue(line.substring(0,start).trim(), line.substring(start + 1, line.length()).trim());
     }
   } 
   try
   {
    JarOutputStream jar = new JarOutputStream(new FileOutputStream(outputFile), man );
    addFilesToZipOutputStream(jar, files);
    jar.flush();
    jar.close();
   }
   catch(Throwable ex)
   {
      throw ex;
   }
   finally
   {
    running=false;
    stop=false;
   }
 }

 public String getName()
 {
  return "Jar";
 } 

 public Map<String, ArchiveProperties> properties()
 {
   Map<String, ArchiveProperties> properties = new HashMap<String, ArchiveProperties>();
   properties.put("Manifest", ArchiveProperties.MULTI_LINE_STRING);
   return properties;
 }
}
